package com.blue.base.security.exception;

import com.alibaba.fastjson.JSON;
import com.blue.base.common.bean.resp.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * security 端点请求异常处理器
 *
 * @author liule
 * @version 1.0
 */
@Slf4j
public class EntryPointRejectHandler implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException {
        log.error("统一认证失败处理器....");
        response.setContentType("application/json;charset=UTF-8");
        response.getWriter().write(JSON.toJSONString(JsonResult.failed("非法请求，禁止访问")));
    }

}
